package com.ukao.cashregister.utils;

import android.content.Context;
import android.hardware.usb.UsbDevice;
import android.hardware.usb.UsbInterface;
import android.hardware.usb.UsbManager;
import android.util.Log;

import java.util.HashMap;
import java.util.Iterator;

/**
 * Created by ukaodev on 2018/3/1.
 */

public class CheckUsbDeviceUtils {




    /**
     * 是否是标签打印机
     *
     */
    public static  boolean   isTSCprinter(   ){
        boolean  isWashprinter=false;
        UsbManager manager = (UsbManager) Utils.getContext().getSystemService(Context.USB_SERVICE);
        HashMap<String, UsbDevice> deviceList = manager.getDeviceList();
        Iterator<UsbDevice> deviceIterator = deviceList.values().iterator();
        StringBuilder sb = new StringBuilder();
        while (deviceIterator.hasNext()) {
            UsbDevice usbDevices = deviceIterator.next();
            int deviceClass = usbDevices.getDeviceClass();
            if (deviceClass == 0) {
                UsbInterface anInterface = usbDevices.getInterface(0);
                int interfaceClass = anInterface.getInterfaceClass();
                if (anInterface.getInterfaceClass() == 7) {
                    if(usbDevices.getVendorId() == 4611 && usbDevices.getProductId() == 370){/*tsc打印机*/
                        isWashprinter=true;

                    }


                } else if (anInterface.getInterfaceClass() == 8) {
                    sb.append("此设备是U盘\n");
                }

            }



        }

        if (!isWashprinter){
            T.show("未连接标签打印机");
        }
        return  isWashprinter;
    }




    /**
     * 是否是isWashprinter打印机
     *
     */
    public static  boolean   isWashprinter(   ){
        boolean  isWashprinter=false;
        UsbManager manager = (UsbManager) Utils.getContext().getSystemService(Context.USB_SERVICE);
        HashMap<String, UsbDevice> deviceList = manager.getDeviceList();
        Iterator<UsbDevice> deviceIterator = deviceList.values().iterator();

        while (deviceIterator.hasNext()) {
            UsbDevice usbDevices = deviceIterator.next();
            int deviceClass = usbDevices.getDeviceClass();
            if (deviceClass == 0) {
                UsbInterface anInterface = usbDevices.getInterface(0);
                if (anInterface.getInterfaceClass() == 7) {
                    if(usbDevices.getVendorId() == 4611 && usbDevices.getProductId() == 294){/*水洗唛打印机*/
                        isWashprinter=true;

                    }else  if (usbDevices.getVendorId() == 4611 && usbDevices.getProductId() == 310){
                        isWashprinter=true;
                    }


                }

            }



        }

        if (!isWashprinter){
            T.show("未连接水洗唛打印机");
        }
        return  isWashprinter;
    }



    /**
     * 是否是isWashprinter  T210E 打印机
     *
     */
    public  static boolean   isT210EWashprinter(   ){
        boolean  isWashprinter=false;
        UsbManager manager = (UsbManager) Utils.getContext().getSystemService(Context.USB_SERVICE);
        HashMap<String, UsbDevice> deviceList = manager.getDeviceList();
        Iterator<UsbDevice> deviceIterator = deviceList.values().iterator();

        while (deviceIterator.hasNext()) {
            UsbDevice usbDevices = deviceIterator.next();
            int deviceClass = usbDevices.getDeviceClass();
            if (deviceClass == 0) {
                UsbInterface anInterface = usbDevices.getInterface(0);

                if (anInterface.getInterfaceClass() == 7) {
                    if(usbDevices.getVendorId() == 4611 && usbDevices.getProductId() == 310){/*水洗唛打印机*/
                        isWashprinter=true;

                    }


                }

            }



        }


        return  isWashprinter;
    }




    /**
     * 是否是小票
     * @return
     */
    public static  boolean   isReceipt(   ){
        boolean  isReceipt=false;
        UsbManager manager = (UsbManager) Utils.getContext().getSystemService(Context.USB_SERVICE);
        HashMap<String, UsbDevice> deviceList = manager.getDeviceList();
        Iterator<UsbDevice> deviceIterator = deviceList.values().iterator();

        while (deviceIterator.hasNext()) {
            UsbDevice usbDevices = deviceIterator.next();
            int deviceClass = usbDevices.getDeviceClass();
            if (deviceClass == 0) {
                UsbInterface anInterface = usbDevices.getInterface(0);
                if (anInterface.getInterfaceClass() == 7) {
                    if (usbDevices.getVendorId() == 34918 && usbDevices.getProductId() == 256) {//"GP-L80160I打印机已成功连接
                        isReceipt=true;

                    }


                }

            }



        }

        if (!isReceipt){
            T.show("未连接小票打印机");
        }
        return  isReceipt;
    }

    /**
     * 是否是连接读卡器
     *
     */
    public static UsbDevice    getConnectCardReader(){


        UsbManager mUsbManager = (UsbManager) Utils.getContext().getSystemService(Context.USB_SERVICE);
        if(mUsbManager!= null) {
            HashMap<String, UsbDevice> deviceHashMap = mUsbManager.getDeviceList();
            Iterator<UsbDevice> iterator = deviceHashMap.values().iterator();
            while (iterator.hasNext()) {
                UsbDevice   device= iterator.next();
                int mVendorId = device.getVendorId();
                int mmProductId = device.getProductId();
                L.i( "vendor id:" +mVendorId+ "  mProductId: " +mmProductId );
                if (mVendorId==65535 && mmProductId == 53) {

                    return device;
                }
            }
        }else {
            Log.i("Debug:","-------------usbManager is null");
        }
        return null;



    }

}
